package com.geostar.constructionland.statistics.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.common.entity.statistics.LogStatistics;
import com.geostar.constructionland.statistics.mapper.LogStaticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 执行记录表操作服务
 *
 * @Author zhu_jiacheng
 * @Date:2020/7/15 13:40
 */
@Service
public class LogStaticService extends ServiceImpl<LogStaticMapper, LogStatistics> {

    @Autowired
    private LogStaticMapper logStaticMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addEntity(LogStatistics logStatistics) {
        logStaticMapper.insert(logStatistics);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateEntity(LogStatistics logStatistics) {
        logStaticMapper.updateById(logStatistics);
    }

    @Deprecated
    public LogStatistics getLogStatistic(String profileId) {
        // 数据库中排序 获取最新的一条执行记录
        return logStaticMapper.getLogStatistics(profileId).stream().findFirst().get();
    }

    public IPage<LogStatistics> getLogStatisticsForPage(int pageNo, int pageNum, String query_fxlx) {
        IPage<LogStatistics> analyseVersionIPage = new Page<>(pageNo, pageNum);
        QueryWrapper<LogStatistics> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(query_fxlx)) {
            queryWrapper.like("fxlx", query_fxlx);
        }
        queryWrapper.orderByDesc("id");
        return logStaticMapper.selectPage(analyseVersionIPage, queryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    public List<Map> executeSql(String sql) {
//        throw new RuntimeException("莫名其妙的错误"); // 模拟sql执行失败 抛出运行时异常
        return logStaticMapper.executeSql(sql);
    }
}
